<template>
  <div class="home" :style="{ 'min-height': minHeight }">
    <van-nav-bar title="编辑地址" left-arrow>
      <template #left>
        <Back></Back>
      </template>
    </van-nav-bar>
    <van-address-edit
      :area-list="areaList"
      show-postal
      show-delete
      show-set-default
      show-search-result
      :address-info="info"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave($event)"
      @delete="onDelete($event)"
    />
  </div>
</template>

<script>
import Back from "../components/Back.vue";
import { areaList } from "@vant/area-data";
export default {
  name: "AddressEdit",
  data() {
    return {
      minHeight: innerHeight + "px",
      tokenString: sessionStorage.getItem("userToken"),
      appkey: "U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=",
      areaList,
      aid: "",
      info: {
        name: "",
        tel: "",
        province: "",
        city: "",
        county: "",
        addressDetail: "",
        postalCode: "",
        isDefault: "",
      },
    };
  },
  methods: {
    onSave(e) {
      //   
      //   
      let obj = {};
      obj.appkey = this.appkey;
      obj.tokenString = this.tokenString;
      obj.aid = this.aid;
      obj.name = e.name;
      obj.tel = e.tel;
      obj.province = e.province;
      obj.city = e.city;
      obj.county = e.county;
      obj.addressDetail = e.addressDetail;
      obj.areaCode = e.areaCode;
      obj.postalCode = e.postalCode;
      obj.isDefault = e.isDefault ? 1 : 0;
      let data = "";
      for (let key in obj) {
        data += `${key}=${obj[key]}&`;
      }
      data = data.slice(0, -1);
      
      this.axios({
        method: "post",
        url: "http://www.kangliuyong.com:10002/editAddress",
        data,
      })
        .then((res) => {
          
        })
        .catch((err) => {
          
        });
    },
    getAddress(aid) {
      let appkey = this.appkey;
      let tokenString = this.tokenString;
      this.axios({
        method: "get",
        url: "http://www.kangliuyong.com:10002/findAddressByAid",
        params: {
          appkey,
          tokenString,
          aid,
        },
      })
        .then((res) => {
          
          let obj = res.data.result[0];
          this.info.name = obj.name;
          this.info.tel = obj.tel;
          this.info.province = obj.province;
          this.info.city = obj.city;
          this.info.county = obj.county;
          this.info.addressDetail = obj.addressDetail;
          this.info.areaCode = obj.areaCode;
          this.info.postalCode = obj.postalCode;
          this.info.isDefault = obj.isDefault === 1 ? true : false;
        })
        .catch((err) => {
          
        });
    },
    onDelete() {
      
      let appkey = this.appkey;
      let tokenString = this.tokenString;
      let aid = this.aid;
      let data = `appkey=${appkey}&tokenString=${tokenString}&aid=${aid}`;
      this.axios({
        method: "post",
        url: "http://www.kangliuyong.com:10002/deleteAddress",
        data,
      })
        .then((res) => {
          
          this.$router.go(-1);
        })
        .catch((err) => {
          
        });
    },
  },
  created() {
    let aid = this.$route.query.aid;
    this.aid = aid;
    
    this.getAddress(aid);
  },
  components: {
    Back,
  },
};
</script>

<style lang="less" scoped>
.home {
  background-color: rgb(245, 245, 245);
}
/deep/.van-button--danger {
  background-color: rgb(12, 52, 186);
  border: 1px solid rgb(12, 52, 186);
}
</style>